﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model;

namespace YiYiTong.Rocket.Business
{
    public class ChLanApiBiz
    {
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <returns></returns>
        public static bool SendMessage(string mobile, string message)
        {
            ChLanReqModel model = new ChLanReqModel();
            model.account = ConfigHelper.AppSetting<string>("ChLanAccount");//API账号，管理后台获取
            model.password = ConfigHelper.AppSetting<string>("ChLanPwd");//API密码，管理后台获取（8-16位）
            model.msg = message; //审核通过的签名以及模板，长度不超过536个字符
            model.phone = mobile;//接收的手机号,单个手机请求

            string postData = JsonHelper.Serialize(model);
            string postUrl = ConfigHelper.AppSetting<string>("ChLanPostUrl");
            LogHelper.Debug("创蓝短信api请求" + postData);

            //请求api接口获取响应
            string result = HttpHelper.HttpPost(postUrl, postData);
            LogHelper.Debug("创蓝短信api响应" + result);
            if (!string.IsNullOrEmpty(result))
            {
                ChLanRespModel respModel = JsonHelper.Deserialize<ChLanRespModel>(result);
                if (respModel.code != "0")
                {
                    LogHelper.Error("创蓝短信发送产生错误：" + respModel.errorMsg);
                    return false;
                }
                else
                    return true;
            }
            else
                return false;
        }
    }
}
